'use strict';

var config = require('./config');
var SMTPServer = require('smtp-server').SMTPServer;

// Setup server
var server = new SMTPServer({

    // log to console
    logger: true,

    // not required but nice-to-have
    banner: 'Welcome to My Awesome SMTP Server',

    // disable STARTTLS to allow authentication in clear text mode
    disabledCommands: ['STARTTLS'],
    // hideSTARTTLS:true,
    // Accept messages up to 10 MB. This is a soft limit
    size: 10 * 1024 * 1024,

    onAuth: function (auth) {
        console.log(auth);
        var username = 'admin';
        var password = '123';
        if (auth['username'] === username && password === auth['password']) {
           return true;
        }
    }


    // Handle message stream
    // onData(stream, session, callback) {
    //     console.log('Streaming message from user %s', session.user.username);
    //     console.log('------------------');
    //     stream.pipe(process.stdout);
    //     stream.on('end', function() {
    //         console.log(''); // ensure linebreak after the message
    //         callback(null, 'Message queued as ' + Date.now()); // accept the message once the stream is ended
    //     });
    // }
});

server.on('error', function (err) {
    console.log('Error occurred');
    console.log(err);
});

// start listening
server.listen(config.server.port, config.server.host);